package com.example.palyerdemo

import android.os.Bundle
import android.widget.SeekBar
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.media3.ui.PlayerView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.palyerdemo.R

class MainActivity : AppCompatActivity() {

    private lateinit var playerView: PlayerView
    private val mediaUrl: String = "https://vdept3.bdstatic.com/mda-rdn8smchc6mxppcd/cae_h264/1745388912666573811/mda-rdn8smchc6mxppcd.mp4?v_from_s=hkapp-haokan-hnb&auth_key=1756292857-0-0-e1a002c9998e3d88792f65148c33b620&bcevod_channel=searchbox_feed&pd=1&cr=0&cd=0&pt=3&logid=0457606739&vid=12671617739791738342&klogid=0457606739&abtest="
    private val segmentedPlayer: SegmentedPlayer by lazy(LazyThreadSafetyMode.NONE) {
        SegmentedPlayer(
            context = this,
            playerView = playerView,
            totalDurationMs = totalDurationMs,
            segmentCount = segmentCount
        ) { logicalPos, logicalDur ->
            tvOverlayTime.text = formatTime(logicalPos) + "/" + formatTime(logicalDur)
        }.also {
            it.setUri(mediaUrl)
            it.attachToLifecycle(this, playWhenStarted = true, releaseOnDestroy = true)
        }
    }
    private lateinit var seekBar: SeekBar
    private lateinit var tvOverlayTime: TextView
    private lateinit var rvSegments: RecyclerView
    private lateinit var segmentAdapter: SegmentAdapter

    private val totalDurationMs: Long = 60_000
    private val segmentCount: Int = 10

    // 进度刷新与跳段逻辑已内聚到 SegmentedPlayer

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        bindViews()
        setupSeekBar()
        setupSegmentsUI()
    }

    private fun bindViews() {
        playerView = findViewById(R.id.playerView)
        seekBar = findViewById(R.id.seekBar)
        tvOverlayTime = findViewById(R.id.tvOverlayTime)
        rvSegments = findViewById(R.id.rvSegments)
        segmentAdapter = SegmentAdapter { index, deleted ->
            segmentedPlayer.setDeleted(index, deleted)
            refreshList()
        }
        rvSegments.layoutManager = LinearLayoutManager(this)
        rvSegments.adapter = segmentAdapter
        // 首次不刷新，等首次访问 segmentedPlayer 后再刷新
    }

    private fun setupSeekBar() {
        segmentedPlayer.attachSeekBar(seekBar)
        // 播放器就绪后再刷新列表数据
        refreshList()
    }

    private fun setupSegmentsUI() {
        refreshList()
    }

    private fun refreshList() {
        segmentAdapter.submitList(segmentedPlayer.snapshotSegments())
    }

    private fun formatTime(ms: Long): String {
        val totalSeconds = (ms / 1000).toInt()
        return formatSecond(totalSeconds)
    }

    private fun formatSecond(totalSeconds: Int): String {
        val m = totalSeconds / 60
        val s = totalSeconds % 60
        return String.format("%02d:%02d", m, s)
    }

    // 释放交由 segmentedPlayer.attachToLifecycle 的 onDestroy 自动处理
}